<?php
namespace app\controller;

use support\Request;
use support\Db;

class UserController
{
    public function data(Request $request)
    {
        $id_default = 1;
        $id = $request->get('id', $id_default);
        $name = Db::table('admin_user')->where('id', $id)->value('name');
        $age = Db::table('admin_user')->where('id', $id)->value('age');
        return response("hello $name, you age is $age");
    }

    public function hData(Request $request)
    {
        $h_data = [];
        $h_data =  Db::table('admin_user')->select('name', 'age')->get();   //获取列数据
        // dump($h_data);
        return json($h_data);
    }

    public function vData(Request $request)
    {
        $v_data = [];
        $v_data = Db::table('admin_user')->where('id', 1)->get();   //获取行数据
        return json($v_data);
    }

    /**
     * 获取指定列 $users = Db::table('user')->select('name', 'email as user_email')->get();
     * 获取一行 $user = Db::table('users')->where('name', 'John')->first();
     * 获取一列 $titles = Db::table('roles')->pluck('title');
     * 获取单个字段值: $email = Db::table('users')->where('name', 'John')->value('email');
     * 去重: $email = Db::table('user')->select('nickname')->distinct()->get();
     * 分块结果:
     * Db::table('users')->orderBy('id')->chunkById(100, function ($users) {
            foreach ($users as $user) {
                //
            }
       });
     * 聚合: count, max，min， avg，sum
     * 判断记录是否存在:
        return Db::table('orders')->where('finalized', 1)->exists();
        return Db::table('orders')->where('finalized', 1)->doesntExist();
     * 原生表达式: selectRaw($expression, $bindings = [])
     * Join语句
     * Uion语句
     * Where语句
     * orderBy语句
     * 随机排序: inRandomOrder
     * groupBy / having
     * offset / limit
     * 插入(可以插入多条): insert
     * 自增ID: insertGetId()
     * 更新: update()
     * 更新或新增: updateOrInsert
     * 自增&自减: increment()、decrement()
     * 删除: delete()
     * 事务: try {... Db::commit();} catch(\Throwable $exception) { Db::rollBack(); }
     * 悲观锁: shareLock() or  lockForUpdate()
     * 调试: dd()  or   dump()
     */
}